import pubchempy as pup import os import time element = [] files =os.listdir("./output") element_z = [f for f in files if os.path.isfile(os.path.join("./output", f))] for i in element_z: a=i.replace(".sdf","") print(a) element.append(a) req = ["MolecularFormula"] for j in element: out = pup.get_properties(req,j,'name') try: fname = "./bunshi/" + j + ".txt" with open(fname,"w") as e: e.write(out[0].get("MolecularFormula")) time.sleep(1) except: fname = "./bunshi/" + j + "__error__.txt" with open(fname,"w") as e: e.write("ERROR!\nPlease check your self.") time.sleep(1)